from pml import Objects

class CallFunction:
    @staticmethod
    def CallFunction(frame, this, func, position, *args):
        if isinstance(func, Objects.object.PMTypeObject):
            print(3453435, func.obj.name)
            ret = func.getMember('__call__')(frame, func.obj, position, this, *args)
            return Objects.object.PMTypeObject(Objects.PMNullObject.PMNullObject().getObject()) if ret is None else ret
        else:
            return func(frame, this if isinstance(this, Objects.object.typeobject) else this.obj, position, *args)

    @staticmethod
    def CallFunctionAndCheckReturnType(frame, this, func, position, *args, type=None):
        ret = CallFunction.CallFunction(frame, this, func, position, *args)
        if not isinstance(ret.obj, Objects.object.typeobject if type is None else type):
            raise TypeError(type(ret))
        return ret
